package com.woniuxy.gobang.ui;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;

import javafx.application.Platform;
import messge.Message;
import messge.PieceMessage;

public class ServerThread extends Thread {
	private FightModeStage fightMode;
	
	public ServerThread(FightModeStage fightMode) {
		this.fightMode=fightMode;
	}
	@Override
	public void run() {
		try {
			ServerSocket server = new ServerSocket(Global.myPort);
			while (true) {
				Socket socket = server.accept();// 阻塞
				InputStream is = socket.getInputStream();
				ObjectInputStream ois= new ObjectInputStream(is);
				Message pieceMessage = (Message) ois.readObject();
                // 更新棋盘，在其他线程中更新JavaFX中的界面，需要使用以下的调用
                Platform.runLater(() -> {
					try {
						fightMode.updateUI(pieceMessage);
					} catch (SQLException e) {
						e.printStackTrace();
					}
				});			
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
